<?php

/**
 * This is the model class for table "{{task}}".
 *
 * The followings are the available columns in table '{{task}}':
 * @property integer $taskid
 * @property string $tcode
 * @property string $subcode
 * @property string $office
 * @property string $task
 * @property string $taskdetail
 * @property string $tco
 * @property string $sco
 * @property string $periodic
 * @property integer $urgency
 * @property integer $complete
 * @property string $group
 * @property integer $estart
 * @property integer $rstart
 * @property integer $eend
 * @property integer $rend
 * @property integer $status
 * @property string $follow_up_date
 * @property string $follow_up_person
 * @property string $remark
 * @property string $created_by
 * @property string $updated_by
 * @property integer $create_time
 * @property integer $update_time
 */
class Task extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Task the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{task}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('urgency, complete, estart, rstart, eend, rend, status, create_time, update_time', 'numerical', 'integerOnly' => true),
            array('tcode, subcode, office, follow_up_person, created_by, updated_by', 'length', 'max' => 100),
            array('task', 'length', 'max' => 255),
            array('tco, sco', 'length', 'max' => 50),
            array('periodic', 'length', 'max' => 10),
            array('group', 'length', 'max' => 11),
            array('taskdetail, follow_up_date, remark', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('taskid, tcode, subcode, office, task, taskdetail, tco, sco, periodic, urgency, complete, group, estart, rstart, eend, rend, status, follow_up_date, follow_up_person, remark, created_by, updated_by, create_time, update_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'User', 'created_by'),
            'class' => array(self::BELONGS_TO, 'Classes', 'tcode'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'taskid' => 'Taskid',
            'tcode' => 'Tcode',
            'subcode' => 'Subcode',
            'office' => 'Office',
            'task' => 'Task',
            'taskdetail' => 'Taskdetail',
            'tco' => 'Tco',
            'sco' => 'Sco',
            'periodic' => 'Periodic',
            'urgency' => 'Urgency',
            'complete' => 'Complete',
            'group' => 'Group',
            'estart' => 'Estart',
            'rstart' => 'Rstart',
            'eend' => 'Eend',
            'rend' => 'Rend',
            'status' => 'Status',
            'follow_up_date' => 'Follow Up Date',
            'follow_up_person' => 'Follow Up Person',
            'remark' => 'Remark',
            'created_by' => 'Created By',
            'updated_by' => 'Updated By',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('taskid', $this->taskid);
        $criteria->compare('tcode', $this->tcode, true);
        $criteria->compare('subcode', $this->subcode, true);
        $criteria->compare('office', $this->office, true);
        $criteria->compare('task', $this->task, true);
        $criteria->compare('taskdetail', $this->taskdetail, true);
        $criteria->compare('tco', $this->tco, true);
        $criteria->compare('sco', $this->sco, true);
        $criteria->compare('periodic', $this->periodic, true);
        $criteria->compare('urgency', $this->urgency);
        $criteria->compare('complete', $this->complete);
        $criteria->compare('group', $this->group, true);
        $criteria->compare('estart', $this->estart);
        $criteria->compare('rstart', $this->rstart);
        $criteria->compare('eend', $this->eend);
        $criteria->compare('rend', $this->rend);
        $criteria->compare('status', $this->status);
        $criteria->compare('follow_up_date', $this->follow_up_date, true);
        $criteria->compare('follow_up_person', $this->follow_up_person, true);
        $criteria->compare('remark', $this->remark, true);
        $criteria->compare('created_by', $this->created_by, true);
        $criteria->compare('updated_by', $this->updated_by, true);
        $criteria->compare('create_time', $this->create_time);
        $criteria->compare('update_time', $this->update_time);

        $session = new CHttpSession;
        $session->open();
        $timeFilter = $session['timeFilter'];

        if ($timeFilter['estart1']) {
            $criteria->addCondition('estart>=' . strtotime($timeFilter['estart1']));
        }
        if ($timeFilter['estart2']) {
            $criteria->addCondition('estart<' . strtotime($timeFilter['estart2']));
        }
        if ($timeFilter['eend1']) {
            $criteria->addCondition('eend>=' . strtotime($timeFilter['eend1']));
        }
        if ($timeFilter['eend2']) {
            $criteria->addCondition('eend<' . strtotime($timeFilter['eend2']));
        }
        if ($timeFilter['rstart1']) {
            $criteria->addCondition('rstart>=' . strtotime($timeFilter['rstart1']));
        }
        if ($timeFilter['rstart2']) {
            $criteria->addCondition('rstart<' . strtotime($timeFilter['rstart2']));
        }
        if ($timeFilter['rend1']) {
            $criteria->addCondition('rend>=' . strtotime($timeFilter['rend1']));
        }
        if ($timeFilter['rend2']) {
            $criteria->addCondition('rend<' . strtotime($timeFilter['rend2']));
        }

        $criteria->with = 'user';
        $criteria->addCondition("t.office='" . Yii::app()->user->office . "'"); //只能取得和当前用户同一office的
        $criteria->addCondition("user.read=0 or user.id=" . Yii::app()->user->id); //用户定义为可见的

        $criteria->order = 'status ASC,urgency DESC,taskid DESC'; //排序条件

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * 给普通人员用的，只能看见自己office的任务
     */
    public function taskList($office) {

        $criteria = new CDbCriteria;

        $criteria->addCondition("id=1"); //查询条件，即where id = 1

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->created_by = $this->updated_by = Yii::app()->user->id;
                $this->create_time = $this->update_time = time();
				$this->follow_up_date = date('Y-m-d H:i:s');
            } else {
                $this->updated_by = Yii::app()->user->id;
                $this->update_time = time();
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 给personDate关联任务用的，只能看见自己office的任务，而且不能看见已经关联过的任务
     */
    public function assignSearch($pdid) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('taskid', $this->taskid);
        $criteria->compare('tcode', $this->tcode, true);
        $criteria->compare('subcode', $this->subcode, true);
        $criteria->compare('task', $this->task, true);
        $criteria->compare('taskdetail', $this->taskdetail, true);
        $criteria->compare('tco', $this->tco, true);
        $criteria->compare('sco', $this->sco, true);
        $criteria->compare('periodic', $this->periodic, true);
        $criteria->compare('urgency', $this->urgency);
        $criteria->compare('complete', $this->complete);
        $criteria->compare('group', $this->group, true);
        $criteria->compare('estart', $this->estart);
        $criteria->compare('rstart', $this->rstart);
        $criteria->compare('eend', $this->eend);
        $criteria->compare('rend', $this->rend);
        $criteria->compare('status', $this->status);
        $criteria->compare('follow_up_person', $this->follow_up_person, true);
        $criteria->compare('remark', $this->remark, true);
        $criteria->compare('created_by', $this->created_by, true);
        $criteria->compare('updated_by', $this->updated_by, true);
        $criteria->compare('create_time', $this->create_time);
        $criteria->compare('update_time', $this->update_time);

        $personTasks = PersonTask::model()->findAll('pdid=:pdID', array('pdID' => (int) $pdid));
        if ($personTasks) {
            $idArr = array();
            foreach ($personTasks as $personTask) {
                $idArr[] = $personTask['tid'];
            }
            $criteria->addNotInCondition('taskid', $idArr); //与上面正好相法，是NOT IN
        }

        $criteria->with = 'user';
        $criteria->addCondition("t.office='" . Yii::app()->user->office . "'"); //只能取得和当前用户同一office的
        $criteria->addCondition("user.read=0 or user.id=" . Yii::app()->user->id); //用户定义为可见的
        $criteria->addCondition("t.status=1"); //open的task
        $criteria->addCondition("!(periodic='O' and complete>=100)"); 

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    //personTask完成时更新关联任务的完成度
    public function updateComplete($tid, $complete) {
        $model = $this->findByPk((int) $tid);
        if ($model === null) {
            return false;
        } else {
            $model->complete = $model->complete + $complete;
            if($model->complete>=100){
                $model->status = 2;
            }
            return $model->save();
        }
    }

    /**
     * 给从template创建任务用的，没有具体的表关系，临时存在了session中
     * idArr是taskid列表
     * type，0为已选择的列表，1为未选择的列表
     */
    public function templateSearch($idArr, $type) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('taskid', $this->taskid);
        $criteria->compare('tcode', $this->tcode, true);
        $criteria->compare('subcode', $this->subcode, true);
        $criteria->compare('task', $this->task, true);
        $criteria->compare('taskdetail', $this->taskdetail, true);
        $criteria->compare('tco', $this->tco, true);
        $criteria->compare('sco', $this->sco, true);
        $criteria->compare('periodic', $this->periodic, true);
        $criteria->compare('urgency', $this->urgency);
        $criteria->compare('complete', $this->complete);
        $criteria->compare('group', $this->group, true);
        $criteria->compare('estart', $this->estart);
        $criteria->compare('rstart', $this->rstart);
        $criteria->compare('eend', $this->eend);
        $criteria->compare('rend', $this->rend);
        $criteria->compare('status', $this->status);
        $criteria->compare('follow_up_person', $this->follow_up_person, true);
        $criteria->compare('remark', $this->remark, true);
        $criteria->compare('created_by', $this->created_by, true);
        $criteria->compare('updated_by', $this->updated_by, true);
        $criteria->compare('create_time', $this->create_time);
        $criteria->compare('update_time', $this->update_time);

        if ($type) {
            $criteria->addNotInCondition('taskid', $idArr); //与上面正好相法，是NOT IN
        } else {
            $criteria->addInCondition('taskid', $idArr);
        }

        $criteria->with = 'user';
        $criteria->addCondition("t.office='" . Yii::app()->user->office . "'"); //只能取得和当前用户同一office的
        $criteria->addCondition("user.read=0 or user.id=" . Yii::app()->user->id); //用户定义为可见的
        $criteria->addCondition("t.status=1"); //状态为open的task
        $criteria->addCondition("!(periodic='O' and complete>=100)");

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 3
                    ),
                ));
    }

}